package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class test extends AppCompatActivity {

    //申明成员变量
    private ImageSwitcher mImagSwitcher;
    private int[] imgRes = {R.drawable.leaf1, R.drawable.leaf2, R.drawable.leaf3};
    private Animation mAnimation;
    private boolean playFlag = false, pauseFlag = false;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        // 启动服务播放背景音乐
        intent = new Intent(test.this, MyIntentService.class);
        String action = MyIntentService.ACTION_MUSIC;
        // 设置action
        intent.setAction(action);
        startService(intent);

        mImagSwitcher = (ImageSwitcher) findViewById(R.id.imagSwitcher);

        mImagSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                // makeView返回的是当前需要显示的ImageView控件，用于填充进ImageSwitcher中
                return new ImageView(test.this);
            }
        });


        mImagSwitcher.postDelayed(new Runnable() {
            int currentIndex = 0;
            //重写run0
            @Override
            public void run() {
                mImagSwitcher.setImageResource(imgRes[currentIndex]);
                if (currentIndex == (imgRes.length - 1))
                    currentIndex = 0;
                else
                    currentIndex++;
                //渐变
                mAnimation = AnimationUtils.loadAnimation(test.this
                        , R.anim.my_alpha);
                mImagSwitcher.startAnimation(mAnimation);

                mImagSwitcher.postDelayed(this, 1000);
            }
        }, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (intent != null){
            // 对于intentService，这一步可能是多余的
            stopService(intent);
        }
    }
}